<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

/**
 * @Helper:: getJsonEncode()
 * @Author: Samrat Khan
 * @params:$data
 * @return
 */
function jsonEncode($data) {
	return json_encode($data);
}

/**
 * @Helper::jsonDecode()
 * @Author:  Samrat Khan
 * @params:$data
 * @return
 */
function jsonDecode($data) {
	return json_decode($data);
}

/**
 * @Helper::setSerialize()
 * @Author: Samrat Khan
 * @params:$data
 * @return
 */
function serializeArr($data) {
	return serialize($data);
}

/**
 * @Helper::unserializeArr()
 * @Author: Samrat Khan
 * @params:$data
 * @return
 */
function unserializeArr($data) {
	return unserialize($data);
}

/**
 * @Helper::base64Encode()
 * @Author: Samrat Khan
 * @params:$data
 * @return
 */
function base64Encode($data) {
	return base64_encode($data);
}

/**
 * @Helper::base64Decode()
 * @Author: Samrat Khan
 * @params:$data
 * @return
 */
function base64Decode($data) {
	return base64_decode($data);
}

/**
 * @Helper::implodeArr()
 * @Author: Samrat Khan
 * @params:$separator
 * @params:$array
 * @return
 */
function implodeArr($separator, $array) {
	return implode($separator, $array);
}

/**
 * @Helper::base64Decode()
 * @Author: Samrat Khan
 * @params:$min
 * @params:$max
 * @return
 */
function getRange($min, $max) {
	return range($min, $max);
}

/**
 * @Helper::base64Decode()
 * @Author: Samrat Khan
 * @params:$min
 * @params:$max
 * @return
 */
function strReplace($match, $slag, $str) {
	return str_replace($match, $slag, $str);
}

/**
 * @Helper::sprtf()
 * @Author: Samrat Khan
 * @params:$num
 * @params:$slag
 * @return
 */
function sprtf($num, $slag) {
	return sprintf('%.' . $slag . 'f', $num);
}

/**
 * @Helper::getDayLists()
 * @Author: Samrat Khan
 * @return
 */
function getDayLists() {

	$day = '';
	$selected = '';
	$Arr = array(
        '01' => "01",
        '02' => "02",
        '03' => "03",
        '04' => "04",
        '05' => "05",
        '06' => "06",
        '07' => "07",
        '08' => "08",
        '09' => "09",
        '10' => "10",
        '11' => "11",
        '12' => "12",
        '13' => "13",
        '14' => "14",
        '15' => "15",
        '16' => "16",
        '17' => "17",
        '18' => "18",
        '19' => "19",
        '20' => "20",
        '21' => "21",
        '22' => "22",
        '23' => "23",
        '24' => "24",
        '25' => "25",
        '26' => "26",
        '27' => "27",
        '28' => "28",
        '29' => "29",
        '30' => "30",
        '31' => "31"
        );

        foreach ($Arr as $key => $values) {

        	if ($values == date('d'))
        	$selected = 'selected="selected"';
        	else
        	$selected = '';

        	$day .= "<option value=\"" . $values . "\"";
        	$day .= $selected;
        	$day .= ">" . $values . "</option>\n";

        	if ($values == date('t'))
        	break;
        }

        return $day;
}

/**
 * @Helper::getMonthLists()
 * @Author: Samrat Khan
 * @return
 */
function getMonthLists() {

	$arr = array(
        '01' => "01",
        '02' => "02",
        '03' => "03",
        '04' => "04",
        '05' => "05",
        '06' => "06",
        '07' => "07",
        '08' => "08",
        '09' => "09",
        '10' => "10",
        '11' => "11",
        '12' => "12"
        );

        $monthlist = '';

        foreach ($arr as $key => $val) {
        	$monthlist .= "<option value=\"$key\"";
        	$monthlist .= ($key == date('m')) ? ' selected="selected"' : '';
        	$monthlist .= ">$val</option>\n";
        }

        return $monthlist;
}

/**
 * @Helper::setFirstLetterCapital()
 * @Author: Samrat Khan
 * @params:$word
 * @return
 */
function setFirstLetterCapital($word) {
	return ucfirst($word);
}

/**
 * @GeneralModel::getToken()
 * @access:public
 * @params:length
 * @Author: Samrat Khan
 * @return $code
 */
function getToken($length = "") {

	$code = uniqid(rand(), true);
	if ($length != "") {
		return substr($code, 0, $length);
	} else
	return $code;
}

/**
 * @Helper::hashSha1()
 * @Author: Samrat Khan
 * @params:$data
 * @return
 */
function hashSha1($data) {
	return sha1($data);
}

/**
 * @Helper::getAdminType()
 * @Author: Samrat Khan
 * @params:$priority
 * @return
 */
function getAdminType($priority) {

	switch ($priority) {
		case 1: $designation = 'Moderators';
		break;
		case 3: $designation = 'Staffs';
		break;
		case 5: $designation = 'Teachers';
		break;
		case 9: $designation = 'Students';
		break;
	}

	return $designation;
}

/**
 * @Helper::getAdminStatus()
 * @Author: Samrat Khan
 * @params:$satus
 * @return
 */
function getAdminStatus($status) {

	switch ($status) {
		case 0: $designation = 'Inactive';
		break;
		case 2: $designation = 'Retired';
		break;
		case 3: $designation = 'Active';
		break;
	}

	return $designation;
}

/**
 * @Helper::getSmsUsers()
 * @Author: Samrat Khan
 * @return
 */
function getSmsUsers() {

	$ci = & get_instance();
	$ci->db->group_by('user_priority');
	$sql = $ci->db->get("sms_users");

	if ($sql->num_rows() > 0) {

		return $sql->result();
	} else
	return '';
}

/**
 * @Common_model::getRecords()
 * @Author: Samrat Khan
 * @access:public
 * @params:$table
 * @params:$where
 * @params:$option
 * @return
 */
function getRecordsHelper($table, $where, $option) {

	$ci = & get_instance();

	if (!empty($where))
	$sql = $ci->db->get_where($table, $where);
	else
	$sql = $ci->db->get($table);

	if ($sql->num_rows() > 0) {

		if ($option == "all") {

			foreach ($sql->result() as $rows) {

				$data[] = $rows;
			}
		} else {

			$data = $sql->row_array();
		}

		return $data;
	} else
	return false;
}


/**
 * @package:GTF
 * @helper::mbConvertEncoding().
 * @Author: Samrat Khan
 */
function mbConvertEncoding($text) {
	return mb_convert_encoding($text, 'ISO-8859-15', 'UTF-8');
}


/**
 * @package:GTF
 * @helper::getArraySum().
 * @Author: Samrat Khan
 */
function getArraySum($array) {
	return array_sum($array);
}

/**
 * @package:GTF
 * @helper::getCount().
 * @Author: Samrat Khan
 */
function getCount($array) {
	return count($array);
}

/**
 * @package:GTF
 * @helper::getArraySum().
 * @Author: Samrat Khan
 */
function getExplode($slag, $str) {
	return explode($slag, $str);
}

/**
 * @package:GTF
 * @helper::getArraySum().
 * @Author: Samrat Khan
 */
function getImplode($slag, $str) {
	return implode($slag, $str);
}

/**
 * @package:GTF
 * @helper::getRound().
 * @Author: Samrat Khan
 */
function getRound($num, $decimal_num) {
	return round($num, $decimal_num);
}

/**
 * @package:GTF
 * @helper::add_js().
 * @Author: Samrat Khan
 */
if (!function_exists('add_js')) {

	function add_js($file='')
	{
		$str = '';
		$ci = &get_instance();
		$header_js  = $ci->config->item('header_js');

		print_r($header_js );

		if(empty($file)){
			return;
		}

		if(is_array($file)){
			if(!is_array($file) && count($file) <= 0){
				return;
			}
			foreach($file AS $item){
				$header_js[] = $item;
			}
			$ci->config->set_item('header_js',$header_js);
		}else{
			$str = $file;
			$header_js[] = $str;
			$ci->config->set_item('header_js',$header_js);
		}
	}

}
/**
 * @package:GTF
 * @helper::add_css().
 * @Author: Samrat Khan
 */
if (!function_exists('add_css')) {

	function add_css($file = '') {
		$str = '';
		$ci = &get_instance();
		$header_css = $ci->config->item('header_css');

		if (empty($file)) {
			return;
		}

		if (is_array($file)) {
			if (!is_array($file) && count($file) <= 0) {
				return;
			}
			foreach ($file AS $item) {
				$header_css[] = $item;
			}
			$ci->config->set_item('header_css', $header_css);
		} else {
			$str = $file;
			$header_css[] = $str;
			$ci->config->set_item('header_css', $header_css);
		}
	}

}
/**
 * @package:GTF
 * @helper::put_headers().
 * @Author: Samrat Khan
 */
if (!function_exists('put_headers')) {

	function put_headers() {
		$str = '';
		$ci = &get_instance();
		$header_css = $ci->config->item('header_css');

		foreach ($header_css AS $item) {
			$str_css[] = $item; /* '<link rel="stylesheet" href="'.base_url().''.$item.'" type="text/css" />'."\n"; */
			$str .= '<link rel="stylesheet" href="' . base_url() . '' . $item . '" type="text/css" />' . "\n";
		}

		return $str;
	}

}
/**
 * @package:GTF
 * @helper::put_footer().
 * @Author: Samrat Khan
 */
if (!function_exists('put_footer')) {

	function put_footer() {
		$str = '';
		$ci = &get_instance();
		$footer_js = $ci->config->item('footer_js');

		foreach ($footer_js AS $item) {
			$str .= '<script  type="text/javascript"  src="' . base_url() . '' . $item . '"></script>' . "\n";
		}

		return $str;
	}

}

/**
 * @package:GTF
 * @helper::setOutPut().
 * @Author: Samrat Khan
 */
function setOutPut() {
	$ci = &get_instance();
	$ci->output->set_output('YES');
}

/**
 * @package:GTF
 * @helper::isSession().
 * @Author: Samrat Khan
 */
function isSession() {
	$ci = &get_instance();
	$isSession = $ci->session->userdata("token");

	return
	($isSession) ? $isSession : 0;
}


/**
 * @package:GTF
 * @helper::isArray().
 * @Author: Samrat Khan
 */
function isArray($array) {
	return is_array($array);
}

/**
 * @package:GTF
 * @helper::isModerator().
 * @Author: Samrat Khan
 */
function isModerator() {
	return (getSession('token') == 1) ? 1 : 0;
}

/**
 * @package:GTF
 * @helper::isTeacher().
 * @Author: Samrat Khan
 */
function isTeacher() {
	return (getPriority() == 5) ? 1 : 0;
}

/**
 * @package:GTF
 * @helper::isStaff().
 * @Author: Samrat Khan
 */
function isStaff() {
	return (getPriority() == 3) ? 1 : 0;
}

/**
 * @package:GTF
 * @helper::isStudent().
 * @Author: Samrat Khan
 */
function isStudent() {
	return (getPriority() == 9) ? 1 : 0;
}

/**
 * @package:GTF
 * @helper::getPriority().
 * @Author: Samrat Khan
 */
function getPriority() {
	$ci = getInstance();
	return $ci->session->userdata("priority");
}

/**
 * @package:GTF
 * @helper::getInstance().
 * @Author: Samrat Khan
 */
function getInstance() {
	$ci = &get_instance();
	return $ci;
}


function loadCommon_model() {
	$ci = getInstance();
	return $ci->load->model("admin/common_model");
}


/**
 * @package:GTF
 * @getOrientedDecision::getMaxArrayValue().
 * @Author: Samrat Khan
 */
function getMaxArrayValue($array) {
	return max($array);
}

/**
 * @package:GTF
 * @getOrientedDecision::getMinArrayValue().
 * @Author: Samrat Khan
 */
function getMinArrayValue($array) {
	return min($array);
}

/**
 * @package:GTF
 * @helper::getTime().
 * @Author: Samrat Khan
 */
function getTime() {
	return date("Y-m-d H:i:s");
}

/**
 * @package:GTF
 * @helper::setRules()
 * @access:private
 * @params:$config
 * @Author: Samrat Khan
 * @return
 */
function setRules($config) {

	$ci = getInstance();
	return $ci->form_validation->set_rules($config);
}
/**
 * @package:GTF
 * @helper::setCookies()
 * @access:private
 * @Author: Samrat Khan
 */
function setCookies($cookie) {
	$ci = getInstance();
	$ci->input->set_cookie($cookie);
}
/**
 * @package:GTF
 * @helper::getCookie()
 * @access:private
 * @Author: Samrat Khan
 * @return:cookie
 */
function getCookie($name) {
	$ci = getInstance();
	return ($ci->input->cookie($name)) ? $ci->input->cookie($name) : 0;
}
/**
 * @package:GTF
 * @helper::storeLogHistory()
 * @params:$data
 * @Author: Samrat Khan
 * @return:void
 */
function storeLogHistory($data){
	$ci = getInstance();
	$ci->common_model->insertRecords($ci->common_model->_logTable, $data);
}
/**
 * @helper::getUserIp()
 * @Author: Samrat Khan
 */
function getUserIp(){
	$ci = getInstance();
	return $ci->input->ip_address();
}

/**
 * @package:GTF
 * @helper::getCurrentsUrl().
 * @Author: Samrat Khan
 */
function getCurrentsUrl(){
	return (isset($_SERVER['HTTP_REFERER'])) ?  $_SERVER['HTTP_REFERER'] : base_url();
}
/**
 * @package:GTF
 * @helper::getCurrentsUrl().
 * @Author: Samrat Khan
 */
function getSession($session = NULL){
	$ci = getInstance();
	$ci->status_ = $ci->session->userdata("token");

	if($session) $session = $ci->session->userdata($session);
	else $session = $ci->session->all_userdata();

	return
	($ci->status_ && $session) ? $session : 0;
}
/**
 * @package:GTF
 * @helper::getCurrentsUrl().
 * @Author: Samrat Khan
 */
function getUserPhoto(){
	$allsession = getSession();
	return $allsession['user_photo'];
}
/**
 * @package:GTF
 * @helper::getCurrentsUrl().
 * @Author: Samrat Khan
 */
function getMesgNum(){

	$ci = getInstance();
	$allsession = getSession();
	$where = array("id" => $allsession['token']);
	$user_infos = $ci->common_model->getRecords($ci->common_model->_usersTable, $where, 's');

	return
	$ci->common_model->getNumRows($ci->common_model->_messageTable, array('account' => $user_infos['mailbox']));
}
/**
 * @package:GTF
 * @helper::getUsers().
 * @Author: Samrat Khan
 */
function getUsers(){
	$ci = getInstance();
	return
	$ci->common_model->getRecords($ci->common_model->_usersTable, '', 'all');
}
/**
 * @package:GTF
 * @helper::getUserById().
 * @Author: Samrat Khan
 */
function getUserById($id){
	$ci = getInstance();
	return
	$ci->common_model->getRecords($ci->common_model->_usersTable,array('id' => $id), 's');
}

/**
 * @package:GTF
 * @helper::getUsersNum()().
 * @Author: Samrat Khan
 */
function getUsersNum(){

	$ci = getInstance();
	return
	$ci->common_model->getNumRows($ci->common_model->_usersTable, '');
}

/**
 * @package:GTF
 * @helper::getUsersNum()().
 * @Author: Samrat Khan
 */
function isForceChangePwd(){
	$allsession = getSession();
	return
	(isset($allsession['force_to_change_password']) && $allsession['force_to_change_password'] == 1) ? 1 : 0;
}
/**
 * @package:GTF
 * @helper::getUserAccessPages().
 * @Author: Samrat Khan
 */
function getUserAccessPages($id){

	$ci = getInstance();
	$sql_pages = $ci->common_model->getRecords($ci->common_model->_accessPagesTable,'', 'all');
	$pages = array();

	foreach ($sql_pages as $lists){

		if(in_array($id, getExplode(',', $lists->user_groups))) {
			$pages[] = $lists;
		}
	}

	return $pages;
}
/**
 * @package: GTF
 * @helper::isUserAllowwedToAccessMenu().
 * @Author: Samrat Khan
 */
function isUserAllowwedToAccessMenu($menu){

	$ci = getInstance();
	$where = array('page_name' => $menu);

	if(getSession('token') != 1){
		if($ci->common_model->isRecordsExists($ci->common_model->_accessPagesTable, $where)){

			$sql_pages = $ci->common_model->getRecords($ci->common_model->_accessPagesTable,$where, 's');

			return
			(in_array(getSession('token'), getExplode(',', $sql_pages['user_groups']))) ? 1 : 0;
		}
		else
		return 0;
	}
	else
	return 1;

}

function switchCaseForJob($status) {
	switch ($status) {
		case 1:
			$statusVar = "Awaiting review";
			break;
		case 2:
			$statusVar = "Jobs in progress";
			break;
		case 3:
			$statusVar = "Jobs on hold";
			break;
		default:
			break;
	}
	return $statusVar;
}

/* End of file helper.php */
/* Location: ./application/helpers/helper.php */
?>